package springbootorderrabbitmqproducer.demo.service;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * @param userid    用户id
     * @param productId 商品id
     * @param num       数量
     * fanout 模式
     */
    public void makOrder(String userid, String productId, int num) {
        //1. 根据商品id查询库存是否充足
        //2. 保存订单
        String orderId = UUID.randomUUID().toString();
        System.err.println("订单生成成功：" + orderId);
        //3. 通过MQ来完成消息的分发0020
        // 参数 1 交换机 参数2：路由key/ 队列名称 参数3  消息内容
        String exchangeName = "fanout_order_exchange";
        String routeKey = "";
        String queue = "";
        rabbitTemplate.convertAndSend(exchangeName, routeKey, orderId);
    }

    /**
     * com.djx.service.direct 模式
     * @param userid
     * @param price
     * @param num
     */
    public void payOrder(String userid, String price, String num) {
        userid = UUID.randomUUID().toString();
        price = "200RMB";
        num = "2";
        String payInfo = "用户id为：" + userid + ",金额为：" + price + "数量为：" + num;
        String exchangeName = "direct_pay_exchange";
        String routeKey = "sms";
        String queue = "";
        rabbitTemplate.convertAndSend(exchangeName, routeKey, payInfo);

    }

    /**
     * topic 模式
     * @param userid
     * @param price
     * @param num
     */
    public void eatOrder(String userid, String price, String num) {
        userid = UUID.randomUUID().toString();
        price = "500RMB";
        num = "6";
        String payInfo = "用户id为：" + userid + " ,吃饭的金额为：" + price + " 数量为：" + num;
        String exchangeName = "topic_eat_exchange";
        String routeKey = "com.weChat.email";
        String queue = "";
        rabbitTemplate.convertAndSend(exchangeName, routeKey, payInfo);
    }

    /**
     * topic 模式
     * @param userid
     * @param price
     * @param num
     */
    public void eatOrder2(String userid, String price, String num) {
        userid = UUID.randomUUID().toString();
        price = "500RMB";
        num = "6";
        String payInfo = "消费者定义的 绑定";
        String exchangeName = "topic_order_exchange";
        String routeKey = "com.weChat.email";
        String queue = "";
        rabbitTemplate.convertAndSend(exchangeName, routeKey, payInfo);
    }

    /**
     * @param userid
     * @param price
     * @param num
     * headers 模式
     */
    public void playOrder(String userid, String price, String num) {
        userid = UUID.randomUUID().toString();
        price = "500RMB";
        num = "6";
        String payInfo = "用户id为：" + userid + " ,吃饭的金额为：" + price + " 数量为：" + num;
        String exchangeName = "headers_play_exchange";
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("queueName", "sms");
        // MessageProperties.setHeader("name","敦佳星");
        messageProperties.setHeader("age","19");
//        MessageProperties.setHeader("sex","男");
        String routeKey = "";
        Message message = new Message(payInfo.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(exchangeName, routeKey, message);
    }
}
